package bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BioServer {
    private static final int PORT = 9999;
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(PORT);
        ExecutorService executorService = Executors.newCachedThreadPool();
        System.out.println("服务器启动成功...");
        while (true) {
            System.out.println("等待连接...");
            Socket accept = socket.accept();
            executorService.execute(() -> {
                try {
                    doConnection(accept);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }

    private static void doConnection(Socket socket) throws IOException {
        byte[] bytes = new byte[1024];
        InputStream inputStream = socket.getInputStream();
        while (true) {
            int read = inputStream.read(bytes, 0, bytes.length);
            if (read == -1) {
                break;
            }
            System.out.println(new String(bytes, 0, read));
        }
        socket.close();
    }
}
